package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Beans.Estate;
import Utils.HibernateUtil;

public class EstateDao {
	private SessionFactory sessionFactory;

	public EstateDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	
	/**
	 * 删除物业公司
	 */
	public void delete(Estate estate) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(estate);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改物业公司
	 */
	public void edit(Estate estate) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(estate);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的物业公司
	 */
	public void save(Estate estate) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(estate);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有物业公司信息
	 */
	public List<Estate> getAllEstate(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Estate.class);
		List<Estate> estates=c.list();
		tr.commit();
		session.close();
		return estates;
	}
	
	/**
	 * 根据省城市返回所有物业公司信息
	 */
	public List<Estate> getEstateByCity(String city){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Estate.class);
//		c.add(Restrictions.eq("city", city));
		List<Estate> estates=c.list();
		tr.commit();
		session.close();
		return estates;
	}
	
//	/**
//	 * 根据代理商名字返回所有代理商信息
//	 */
//	public List<Estate> getEstateByName(String name){
//		Session session=sessionFactory.openSession();
//		tr=session.beginTransaction();
//		Criteria c=session.createCriteria(Estate.class);
//		c.add(Restrictions.eq("name", name));
//		List<Estate> estates=c.list();
//		tr.commit();
//		session.close();
//		return estates;
//	}

	/**
	 * 通过代理商ID返回指定物业公司信息
	 * 
	 * @return
	 */
	public Estate getEstateById(int estateId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Estate estate = (Estate) session.get(Estate.class, estateId);
		tr.commit();
		session.close();
		return estate;
	}
}
